package com.spdzen.service.impl;

import com.spdzen.model.News;
import com.github.pagehelper.PageHelper;
import com.spdzen.mapper.NewsMapper;
import com.spdzen.service.NewsService;
import com.spdzen.support.PageNumParam;
import com.spdzen.support.PageOffsetParam;
import com.spdzen.support.PageUtils;
import com.spdzen.support.SimplePage;
import com.spdzen.support.SimpleServiceImpl;
import com.spdzen.vo.NewsAdminVO;
import com.spdzen.vo.search.NewsAdminSearch;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 新闻 服务实现类
 */
@Service("newsService")
public class NewsServiceImpl extends SimpleServiceImpl<NewsMapper, News> implements NewsService {
	@Override
	public SimplePage<NewsAdminVO> selectPage(PageOffsetParam param, NewsAdminSearch search) {
		return selectPage(PageUtils.toPageNumParam(param), search);
	}

	@Override
	public SimplePage<NewsAdminVO> selectPage(PageNumParam param, NewsAdminSearch search) {
		int pageNum = PageUtils.checkPageNum(param.getPageNum());
		int pageSize = PageUtils.checkPageSize(param.getPageSize());

		PageHelper.startPage(pageNum, pageSize);
		
		List<NewsAdminVO> rows = baseMapper.selectAdminVO(search);
		
		return PageUtils.toSimplePage(rows);
	}

	@Override
	public NewsAdminVO selectAdminVOById(Long id) {
		NewsAdminSearch search = new NewsAdminSearch();
		search.setId(id);
		
		List<NewsAdminVO> rows = baseMapper.selectAdminVO(search);
		
		if(rows == null || rows.size() == 0){
			return null;
		}else{
			return rows.get(0);
		}
	}
}
